package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductAttributeValueDTO;
import com.wgh.product.entity.shop.ProductAttributeValue;
import com.wgh.product.service.ProductAttributeValueService;
import com.wgh.product.converter.ProductAttributeValueConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性值控制器
 * 提供商品属性值相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-attribute-value")
public class ProductAttributeValueController {

    @Autowired
    private ProductAttributeValueService productAttributeValueService;
    
    @Autowired
    private ProductAttributeValueConverter productAttributeValueConverter;
    
    /**
     * 根据属性ID查询属性值列表
     */
    @GetMapping("/attribute/{attributeId}")
    public Result<List<ProductAttributeValueDTO>> getAttributeValuesByAttributeId(@PathVariable Long attributeId) {
        try {
            List<ProductAttributeValue> attributeValues = productAttributeValueService.getAttributeValuesByAttributeId(attributeId);
            List<ProductAttributeValueDTO> attributeValueDTOs = productAttributeValueConverter.toDTOList(attributeValues);
            return Result.success(attributeValueDTOs);
        } catch (Exception e) {
            return Result.error("获取属性值列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据分类ID查询属性值列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<ProductAttributeValueDTO>> getAttributeValuesByCategoryId(@PathVariable Long categoryId) {
        try {
            List<ProductAttributeValue> attributeValues = productAttributeValueService.getAttributeValuesByCategoryId(categoryId);
            List<ProductAttributeValueDTO> attributeValueDTOs = productAttributeValueConverter.toDTOList(attributeValues);
            return Result.success(attributeValueDTOs);
        } catch (Exception e) {
            return Result.error("获取分类属性值列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据属性ID和值名称查询属性值
     */
    @GetMapping("/check")
    public Result<ProductAttributeValueDTO> getAttributeValueByAttributeIdAndValue(
            @RequestParam Long attributeId,
            @RequestParam String value) {
        try {
            ProductAttributeValue attributeValue = productAttributeValueService.getAttributeValueByAttributeIdAndValue(attributeId, value);
            ProductAttributeValueDTO attributeValueDTO = productAttributeValueConverter.toDTO(attributeValue);
            return Result.success(attributeValueDTO);
        } catch (Exception e) {
            return Result.error("查询属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增属性值
     */
    @PostMapping
    public Result<Boolean> addAttributeValue(@RequestBody ProductAttributeValueDTO productAttributeValueDTO) {
        try {
            ProductAttributeValue attributeValue = productAttributeValueConverter.toEntity(productAttributeValueDTO);
            boolean result = productAttributeValueService.addAttributeValue(attributeValue);
            return result ? Result.success(true) : Result.error("新增属性值失败");
        } catch (Exception e) {
            return Result.error("新增属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量新增属性值
     */
    @PostMapping("/batch")
    public Result<Boolean> batchAddAttributeValues(@RequestBody List<ProductAttributeValueDTO> productAttributeValueDTOs) {
        try {
            List<ProductAttributeValue> attributeValues = productAttributeValueConverter.toEntityList(productAttributeValueDTOs);
            boolean result = productAttributeValueService.batchAddAttributeValues(attributeValues);
            return result ? Result.success(true) : Result.error("批量新增属性值失败");
        } catch (Exception e) {
            return Result.error("批量新增属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新属性值
     */
    @PutMapping
    public Result<Boolean> updateAttributeValue(@RequestBody ProductAttributeValueDTO productAttributeValueDTO) {
        try {
            ProductAttributeValue attributeValue = productAttributeValueConverter.toEntity(productAttributeValueDTO);
            boolean result = productAttributeValueService.updateAttributeValue(attributeValue);
            return result ? Result.success(true) : Result.error("更新属性值失败");
        } catch (Exception e) {
            return Result.error("更新属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据属性ID删除属性值
     */
    @DeleteMapping("/attribute/{attributeId}")
    public Result<Boolean> deleteAttributeValuesByAttributeId(@PathVariable Long attributeId) {
        try {
            boolean result = productAttributeValueService.deleteAttributeValuesByAttributeId(attributeId);
            return result ? Result.success(true) : Result.error("删除属性值失败");
        } catch (Exception e) {
            return Result.error("删除属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据分类ID删除属性值
     */
    @DeleteMapping("/category/{categoryId}")
    public Result<Boolean> deleteAttributeValuesByCategoryId(@PathVariable Long categoryId) {
        try {
            boolean result = productAttributeValueService.deleteAttributeValuesByCategoryId(categoryId);
            return result ? Result.success(true) : Result.error("删除分类属性值失败");
        } catch (Exception e) {
            return Result.error("删除分类属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据属性ID列表查询属性值列表
     */
    @GetMapping("/attributes")
    public Result<List<ProductAttributeValueDTO>> getAttributeValuesByAttributeIds(@RequestParam List<Long> attributeIds) {
        try {
            List<ProductAttributeValue> attributeValues = productAttributeValueService.getAttributeValuesByAttributeIds(attributeIds);
            List<ProductAttributeValueDTO> attributeValueDTOs = productAttributeValueConverter.toDTOList(attributeValues);
            return Result.success(attributeValueDTOs);
        } catch (Exception e) {
            return Result.error("获取多属性的属性值列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 检查属性值是否存在
     */
    @GetMapping("/exists")
    public Result<Boolean> checkAttributeValueExists(
            @RequestParam Long attributeId,
            @RequestParam String value) {
        try {
            boolean exists = productAttributeValueService.checkAttributeValueExists(attributeId, value);
            return Result.success(exists);
        } catch (Exception e) {
            return Result.error("检查属性值是否存在失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询属性值详情
     */
    @GetMapping("/{id}")
    public Result<ProductAttributeValueDTO> getAttributeValueById(@PathVariable Long id) {
        try {
            ProductAttributeValue attributeValue = productAttributeValueService.getById(id);
            if (attributeValue == null) {
                return Result.success(null);
            }
            ProductAttributeValueDTO attributeValueDTO = productAttributeValueConverter.toDTO(attributeValue);
            return Result.success(attributeValueDTO);
        } catch (Exception e) {
            return Result.error("获取属性值详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID列表批量查询属性值
     */
    @GetMapping("/batch")
    public Result<List<ProductAttributeValueDTO>> getAttributeValuesByIds(@RequestParam List<Long> ids) {
        try {
            List<ProductAttributeValue> attributeValues = productAttributeValueService.listByIds(ids);
            List<ProductAttributeValueDTO> attributeValueDTOs = productAttributeValueConverter.toDTOList(attributeValues);
            return Result.success(attributeValueDTOs);
        } catch (Exception e) {
            return Result.error("批量查询属性值失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除属性值
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAttributeValue(@PathVariable Long id) {
        try {
            boolean result = productAttributeValueService.removeById(id);
            return result ? Result.success(true) : Result.error("删除属性值失败");
        } catch (Exception e) {
            return Result.error("删除属性值失败：" + e.getMessage());
        }
    }
}